<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>添加岗位</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<link rel="stylesheet" href="../../libme/modules/dtree/dtree.css">
	<link rel="stylesheet" href="../../libme/modules/dtree/font/dtreefont.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--form表单-->
	<!--<form class="layui-form layui-form-pane ok-form">-->
	<form class="layui-form ok-form" lay-filter="post-add">
		<input type="hidden" name="postId" id="postId" value="">
		<div class="layui-form-item">
			<label class="layui-form-label">岗位名称</label>
			<div class="layui-input-block">
				<input type="text" name="postName" placeholder="岗位名称" autocomplete="off"
					   class="layui-input" lay-verify="required" lay-verType="tips" id="postName">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">英文名称</label>
			<div class="layui-input-block">
				<input type="text" name="postCode" placeholder="岗位英文名称"
					   autocomplete="off" class="layui-input" lay-verify="required" lay-verType="tips" id="postCode">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">排序号码</label>
			<div class="layui-input-block">
				<input type="text" name="sortno" placeholder="排序号码" autocomplete="off" class="layui-input" id="sortno">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-block">
				<input type="checkbox" name="postStatus" lay-skin="switch" lay-text="启用|停用">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-block">
				<input type="text" name="remark" placeholder="用户什么也没有留下" autocomplete="off" class="layui-input">
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script src="../../libme/pages/body.js"></script>
<script src="../../libme/script/pinyin.js"></script>
<script type="text/javascript">
	layui.use(["element", "form", "jquery", "okMock", "api", "dtree"], function () {
		let form = layui.form;
        let dtree = layui.dtree;
        let okMock = layui.okMock;
        let $ = layui.jquery;
        let api = layui.api;
        okLoading.close();

        var SUCCESS = 'SUCCESS';

		// tree.render({
        // 	elem: "#permissionTree",
        // 	//data: okMock.api.permission.list,
        // 	data: data,//从本地 localStorage 中取值
        // 	showCheckbox: true
        // });

		form.verify({
            urlPath: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z0-9_:]+$").test(value)){
                    return '权限标识代号不能有特殊字符';
                }
                if(/^\d+$/.test(value)){
                    return '权限标识代号不能使用数字';
                }
                if(!/^([A-Za-z]+):([A-Za-z]+):([A-Za-z]+)$/.test(value)){
                    return '权限标识代号不符合要求';
				}
            }
		})
        form.on("submit(add)", function (data) {
			// TODO 权限节点校验
			var doReq = $("#postId").val().trim() == '' ? okMock.api.post.create : okMock.api.post.update;
			data.field.postStatus == undefined ? data.field.postStatus = '1' : data.field.postStatus = '0';
			api.request({
				url: doReq,
				where: data.field,
				dataType: api.jsonType.ujson
			},function (resp, stat) {
                api.greenTickMsg("添加成功", function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            })
			return false;
		});

        $('#postName').blur(function () {
			var name = $(this).val().trim();
			var that = this;
			$.ajax({
				url:okMock.api.post.checkExists,
				data:{postName: name},
				type:'post',
				dataType:'text',
				success: function (res, status) {
				    if(res === SUCCESS) {
                        api.tips("岗位名称已经存在，请更换新的名称。", that, 'd');
                    }
                }
			})
        });

        $('input[name="postName"]').keyup(function (){
            $('input[name="postCode"]').val(pinyin.getFullChars($('input[name="postName"]').val()));
        });


        $(function () {//页面加载

        })
	})
</script>
</body>
</html>
